package my.algo.graph;

import my.algo.data.LinkedList;

public class EdgeWeightedDigraph {

    private final int v;
    private int e;

    private final LinkedList<DirectedEdge>[] adj;

    public EdgeWeightedDigraph(int v) {
        this.v = v;
        this.e = 0;
        this.adj = new LinkedList[v];

        for (int i = 0; i < v; i++) {
            adj[i] = new LinkedList<>();
        }
    }

    public int v() {
        return v;
    }

    public int e() {
        return e;
    }

    public void addEdge(DirectedEdge edge) {
        adj[edge.from()].add(edge);
        e++;
    }

    public Iterable<DirectedEdge> adj(int v) {
        return adj[v];
    }

    public Iterable<DirectedEdge> edges() {
        LinkedList<DirectedEdge> bag = new LinkedList<>();
        for (int i = 0; i < v; i++) {
            for (DirectedEdge edge : adj[v]) {
                bag.add(edge);
            }
        }
        return bag;
    }
}
